<?php 


/* function getCountryList() {
	$field = 'country_' . LANG_SET;
	$field = str_replace('-', '_', $field);
	$country = M('Country')->field("country_code, {$field} AS country_name")->where($where)->select();
	
	return $country;
} *///

/*返回系统变量*/
function get_sys_var($key) {
	$sys = F('sys_var');
	if (empty($sys)) {
		$sys = M('SysVar')->getField('sys_var, sys_content');
		F('sys_var', $sys);
	} 
	return $sys[$key];
}


// 获取IP地址
function my_get_client_ip($type = 0) {
	$type       =  $type ? 1 : 0;
	static $ip  =   NULL;
	if ($ip !== NULL) return $ip[$type];
	if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下，获取客户端真实IP
		$ip=$_SERVER['HTTP_X_REAL_IP'];
	}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
		$ip     =   $_SERVER['HTTP_CLIENT_IP'];
	}elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
		$arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
		$pos    =   array_search('unknown',$arr);
		if(false !== $pos) unset($arr[$pos]);
		$ip     =   trim($arr[0]);
	}elseif (isset($_SERVER['REMOTE_ADDR'])) {
		$ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
	}else{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	// IP地址合法验证
	$long = sprintf("%u",ip2long($ip));
	$ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
	return $ip[$type];
}

// 获取验证码
function get_verify_code () {
	$Verify =     new \Think\Verify();
	$Verify->fontSize = 25;
	$Verify->length   = 4;
	$Verify->useNoise = false;
	$Verify->useCurve = false;
	$Verify->codeSet = '0123456789';
	$Verify->entry();
}

// 检测验证码
function check_verify($code, $id = ''){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

// 密码统一加密
function enter_password($password) { 
	return md5(md5($password)  . C('PWD_KEY') );
}


// utf-8 中文截取字符串
function my_substr($str, $len) {
	
	if (LANG_SET == 'zh-cn') {
		if (mb_strlen($str , 'utf-8') > $len) {
			return mb_substr($str, 0, $len, 'utf-8') . '...';
		}
	} else if (LANG_SET == 'en-us') {
		if (strlen($str) > $len) {
			return substr($str, 0, $len) . '...';
		}
	}
	
	return $str;
}

function _logs($data, $file = 'logs_'){
	$year   = date("Y");
	$month  = date("m");
	$dir    = ROOT_PATH . 'test_logs/' . $year . '/' . $month . '/';
	if(!is_dir($dir)) {
		mkdir($dir,0755,true);
	}
	$file = $dir . $file . date('Y-m-d').'.txt';
	@file_put_contents($file, '----------------' . date('H:i:s') . '--------------------'.PHP_EOL.var_export($data, true).PHP_EOL, FILE_APPEND);
}

function updata(){
	$upload = new \Think\Upload();// 实例化上传类
	$upload->maxSize   =     10485760 ;// 设置附件上传大小图片设置的大小为10m
	$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	/*   $upload->rootPath  =     '.\Public'; // 设置附件上传根目录
	 $upload->savePath  =     'image'; // 设置附件上传（子）目录 */

	$upload->rootPath   =   THINK_PATH;
	$upload->savePath   =   '../Public/images/';
	$upload->autoSub  =    false;
	// 上传文件
	$info   =   $upload->upload();
	return $info;
}


?>